import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken} from '@/utils/token.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout',
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: 'Login' */ '@/views/Login'),
    //
    beforeEnter: (to, from, next) => {
      if( getToken()?.length > 0 ) {
        next('/')
      } else {
        next()
      }
    }
  },
  {
    path: '/layout',
    component: () => import(/* webpackChunkName: 'Layout' */ '@/views/Layout'),
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName: 'Home' */ '@/views/Home'),
        meta: {
          scrollT: 0  //保存首页离开时滚动条位置
        }
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName: 'User' */ '@/views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import(/* webpackChunkName: 'Search' */ '@/views/Search')
  },
  {
    path: '/search_result/:kw',
    component: () => import(/* webpackChunkName: 'SearchResult' */ '@/views/Search/SearchResult')
  },
  {
    path: '/detail',
    component: () => import(/* webpackChunkName: 'ArticleDetail' */ '@/views/ArticleDetail')
  },
  {
    path: '/user_edit',
    component: () => import(/* webpackChunkName: 'UserEdit' */ '@/views/User/UserEdit')
  },
  {
    path: '/chat',
    component: () => import(/* webpackChunkName: 'Chat' */ '@/views/Chat')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


/* router.beforeEach((to, from, next) => {
  if( getToken()?.length > 0 && to.path === '/login') {
    next('/')
  } else {
    next()
  }
}) */


export default router
